package com.hm.ipay.service.impl;

import com.hm.ipay.dao.PushRecordMapper;
import com.hm.ipay.dao.UserAccountMapper;
import com.hm.ipay.domain.result.ExceptionMsg;
import com.hm.ipay.domain.result.ResponseData;
import com.hm.ipay.domain.result.ResponseDataList;
import com.hm.ipay.entity.PushRecord;
import com.hm.ipay.entity.UserAccount;
import com.hm.ipay.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserAccountMapper userAccountMapper;

    @Autowired
    PushRecordMapper pushRecordMapper;

    @Override
    public Long register(UserAccount userAccount) {
        return userAccountMapper.insertSelective(userAccount);
        //System.out.println(userAccount.getId());
    }

    @Override
    public UserAccount login(String tel) {
        return userAccountMapper.selectByTel(tel);
    }

    @Override
    public UserAccount findInvitor(String inviteCode) {
        return userAccountMapper.selectByInviteCode(inviteCode);
    }

    @Override
    public void resetPassword(String mobile, String password) {
        UserAccount account = userAccountMapper.selectByTel(mobile);
        account.setPassword(password);
        userAccountMapper.updateByPrimaryKeySelective(account);
    }

    @Override
    public int insertMessage(PushRecord record) {
        return pushRecordMapper.insertSelective(record);
    }

    @Override
    public ResponseDataList bgMessageList(Integer limit, Integer offset) {
        Map map = new HashMap();
        if (StringUtils.isEmpty(limit)) {
            limit = 20;
        }
        if (StringUtils.isEmpty(offset)) {
            offset = 0;
        }
        map.put("limit", limit);
        map.put("offset", offset);
        List<PushRecord> records = pushRecordMapper.selectAllRecord(map);
        int record_count = pushRecordMapper.countAllRecord();
        return new ResponseDataList(ExceptionMsg.SUCCESS, limit, offset, record_count, records);
    }

    @Override
    public ResponseDataList messageList(Integer limit, Integer offset) {
        Date now = new Date();
        Map map = new HashMap();
        if (StringUtils.isEmpty(limit)) {
            limit = 20;
        }
        if (StringUtils.isEmpty(offset)) {
            offset = 0;
        }
        map.put("limit", limit);
        map.put("offset", offset);
        List<PushRecord> records = pushRecordMapper.selectAllRecord(map);
        List<PushRecord> messages = new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            if (StringUtils.isEmpty(records.get(i).getScheduleTime())) {
                messages.add(records.get(i));
            } else {
                Date scheduleTime = records.get(i).getScheduleTime();
                scheduleTime.setHours(records.get(i).getScheduleTime().getHours() - 8);
                if (scheduleTime.after(now)) {
                    messages.add(records.get(i));
                }
            }
        }
        int record_count = messages.size();
        return new ResponseDataList(ExceptionMsg.SUCCESS, limit, offset, record_count, messages);
    }

    @Override
    public ResponseData delMessage(Long id) {
        pushRecordMapper.deleteByPrimaryKey(id);
        return new ResponseData(ExceptionMsg.SUCCESS);
    }

    @Override
    public ResponseData getMessage(Long id) {
        PushRecord record = pushRecordMapper.selectByPrimaryKey(id);
        return new ResponseData(ExceptionMsg.SUCCESS, record);
    }

    @Override
    public ResponseData updateMessage(PushRecord record) {
        PushRecord pushRecord = pushRecordMapper.selectByPrimaryKey(record.getId());
        pushRecord.setTitle(record.getTitle());
        pushRecord.setContent(record.getContent());
        pushRecordMapper.updateByPrimaryKeyWithBLOBs(pushRecord);
        return new ResponseData(ExceptionMsg.SUCCESS, pushRecord);
    }

    @Override
    public ResponseData setRegistrationId(Long userId, String regId) {
        Map map = new HashMap();
        map.put("id", userId);
        map.put("registrationId", regId);
        userAccountMapper.setRegId(map);
        return new ResponseData(ExceptionMsg.SUCCESS);
    }
}
